package megadrum.mididata;

import megadrum.Common;
import java.io.*;
import javax.swing.JFileChooser;
import java.awt.Component;
import javax.swing.filechooser.FileFilter;

public abstract class SysexFile {

    public static String getPath(Component focus, String title, String extension, boolean openDialog) {
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogTitle(title);
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.addChoosableFileFilter(new MyFileFilter(null, extension));
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int respuesta;
        if (openDialog) {
            respuesta = jfc.showOpenDialog(focus);
        } else {
            respuesta = jfc.showSaveDialog(focus);
        }
        if (respuesta == JFileChooser.APPROVE_OPTION) {
            File f1 = jfc.getSelectedFile();
            String path = f1.getPath();
            if (path != null && !openDialog && !path.substring(path.length() - extension.length()).toLowerCase().equals(extension.toLowerCase())) {
                path = path + "." + extension;
            }
            return path;
        } else {
            return null;
        }
    }

    public static byte[] loadFile(Component focus, String title, String extension) {
        String path = getPath(focus, title, extension, true);
        if (path != null) {
            return loadFile(path);
        } else {
            return null;
        }
    }

    public static byte[] loadFile(String path) {
        try {
            File file = new File(path);
            byte buffer[] = new byte[(int) file.length()];
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
            input.read(buffer, 0, buffer.length);
            input.close();
            return buffer;
        } catch (Exception e) {
            return new byte[0];
        }
    }

    public static int saveFile(Component focus, String title, String extension, byte[] data) {
        String path = getPath(focus, title, extension, false);
        if (path != null) {
            return saveFile(data, path);
        } else {
            return Common.CANCEL;
        }
    }

    public static int saveFile(byte[] data, String path) {
        try {
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(path));
            output.write(data);
            output.close();
            return Common.OK;
        } catch (Exception e) {
            return Common.ERROR;
        }
    }
}

class MyFileFilter extends FileFilter {

    String name;
    String extension;

    public MyFileFilter(String s, String s1) {
        super();
        name = s;
        extension = s1;
    }

    public boolean accept(File f) {
        if (f.isDirectory() || f.getName().substring(f.getName().length() - extension.length()).toLowerCase().equals(extension)) {
            return true;
        } else {
            return false;
        }
    }

    public String getDescription() {
        if (name == null) {
            return "*." + extension;
        } else {
            return name + "." + extension;
        }
    }
}